// pages/myorder/myorderlist/myorderlist.js
import {
  orderSearch,
  tiXingFaHuo,
  printerSend,
  trashOrder
} from '../../../utils/networkRequest.js'
import {
  errorMsg
} from '../../../utils/tool.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    dataSource: [],
    keyword: "",
    isShowDetail: true,
    printering: false,
    page_no:1,
    
  },
  onLoad: function() {},
  getkeyword: function(e) {
    this.setData({
      keyword: e.detail.value,

    })
  },
  onShow:function(){
    if (this.data.keyword){
      this.onsearch();
    } 
  },
  //搜索
  onsearch: function(page,isPush) {
    debugger;
    let self = this;
    let {
      keyword,
    } = this.data;
    let page_no = !isNaN(page) ? page : 1;
    orderSearch(keyword, "", page_no, (r) => {
      if (r.code == 0) {
        self.setData({
          dataSource: isPush ? self.data.dataSource.concat(r.data.orders) : r.data.orders,
          proxies: r.proxies,
          last_page: r.pager.last_page,
          page_no: page_no,
        })
      } else {
        errorMsg(r.message)
      }
    })
  },
  preImg: function(e) {
    let src = e.currentTarget.dataset.src
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: [src] // 需要预览的图片http链接列表
    })
  },


  //下啦刷新
  onPullDownRefresh: function () {
    let self = this; 
    let page_no = 1;


    self.onsearch(page_no);
    wx.stopPullDownRefresh() 
  },
  //上拉加载
  onReachBottom: function () {
    var self = this;
    let { 
      page_no,
      last_page, 
    } = this.data;

    

    if (last_page <= page_no) {
      wx.showToast({
        icon: "none",
        title: '没有更多数据了.'
      })
    } else {
      page_no = parseInt(page_no) + 1;
      self.onsearch(page_no,true);
       
    }

  },


  goSendOrder: function(e) {
    let oid = e.currentTarget.dataset.oid
    let curTab = e.currentTarget.dataset.state
    if (curTab == 1) {
      wx.navigateTo({
        url: '../sendOrder/sendOrder?oid=' + oid,
      })
    } else if (curTab == 2) {
      wx.navigateTo({
        url: '../sendOrder/sendOrder?oid=' + oid,
      })
    } else if (curTab == 3) {
      wx.navigateTo({
        url: '../sendOrder/sendOrder?oid=' + oid,
      })
    } else if (curTab == 4) {
      wx.navigateTo({
        url: '../orderdetail/orderdetail?oid=' + oid,
      })
    } else if (curTab == 5) {
      wx.navigateTo({
        url: '../orderdetail/orderdetail?oid=' + oid,
      })
    }
  },

  //图片预览
  previewImage: function(e) {
    var current = e.currentTarget.dataset.src;
    wx.previewImage({
      current: current[0], // 当前显示图片的http链接		  	
      urls: current // 需要预览的图片http链接列表	
    })
  },

  //提醒发货
  onTiXing: function(e) {
    let id = e.currentTarget.dataset.id;
    tiXingFaHuo(id, (r) => {
      if (r.code == 0) {
        errorMsg(r.message)
      } else {
        errorMsg(r.message)
      }
    })

  },

  //订单补打
  orderBuDa: function(e) {
    let id = e.currentTarget.dataset.id
    let {
      printering
    } = this.data;
    let self = this;

    wx.showModal({
      title: '快递面单补打',
      content: '确定要补打该订单的快递面单吗？',
      showCancel: true, //是否显示取消按钮 
      confirmColor: '#46d5a4', //确定文字的颜色
      success: function(res) {
        if (!res.cancel) {
          if (!printering) {
            self.setData({
              printering: true,
            })
            printerSend(id, (r) => {
              if (r.code == 0) {
                self.setData({
                  printering: false,
                })
                errorMsg(r.message);
              } else {
                self.setData({
                  printering: false,
                })
                errorMsg(r.message);
              }
            })
          }
        }
      },
      fail: function(res) {}, //接口调用失败的回调函数
      complete: function(res) {}, //接口调用结束的回调函数（调用成功、失败都会执行）
    })

  },

  //作废订单
  trashOrder: function(e) {
    let id = e.currentTarget.dataset.id
    let curTab = e.currentTarget.dataset.state
    let index = e.currentTarget.dataset.index
    let self = this;
    wx.showModal({
      title: '作废订单',
      content: '确定要作废该订单吗？',
      showCancel: true, //是否显示取消按钮 
      confirmColor: '#ff6a6a', //确定文字的颜色
      success: function(res) {
        if (!res.cancel) {
          //点击同意
          trashOrder(id, curTab, (r) => {
            debugger;
            if (r.code == 0) {
              self.onsearch();
              errorMsg("作废成功")
            } else {
              errorMsg(r.message)
            }

          })

        }
      },
      fail: function(res) {}, //接口调用失败的回调函数
      complete: function(res) {}, //接口调用结束的回调函数（调用成功、失败都会执行）
    })

  },
})